﻿using System;
using System.Threading.Tasks;
using Coding4Fun.Maelstrom.Common;
using Windows.Foundation;
using Windows.Storage.Streams;

namespace Coding4Fun.Maelstrom.Communication
{
	public sealed class UserInputPacket : IGamePacket
	{
		public PacketType Type { get { return PacketType.UserInputPacket; } }

		public UserInputCommand Command { get; set; }
		public Point3 Position { get; set; }

		public UserInputPacket()
		{
			Position = new Point3();
		}

		public IAsyncAction FromDataReaderAsync(DataReader reader)
		{
			return FromDataReaderAsyncInternal(reader).AsAsyncAction();
		}

		private async Task FromDataReaderAsyncInternal(DataReader reader)
		{
			await reader.LoadAsync(sizeof(int));
			Command = (UserInputCommand)reader.ReadInt32();

			Point3 position = new Point3();
			await reader.LoadAsync(sizeof(double) * 3);
			position.X = (float)reader.ReadDouble();
			position.Y = (float)reader.ReadDouble();
			position.Z = (float)reader.ReadDouble();
			Position = position;
		}

		public void WritePacket(DataWriter writer)
		{
			writer.WriteInt32((int)Command);

			writer.WriteDouble(Position.X);
			writer.WriteDouble(Position.Y);
			writer.WriteDouble(Position.Z);
		}
	}
}
